//
//  HotSearchViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/18.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "HotSearchViewController.h"
#import "HostTableViewCell.h"
#import "ActiveTableViewCell.h"
#import "NSString+URLEncode.h"
#import "CDMNetTool.h"
#import "UIImageView+WebCache.h"

// model
#import "HotSearchRootClass.h"
#import "HotSerchData.h"
#import "HotSerchOrglist.h"
#import "HotSerchProjlist.h"
#import "ShowMoreTableViewCell.h"
#import "ShowMoreViewController.h"
#import "DetailsViewController.h"
#import "FoundDetailsViewController.h"
#define kTableViewCellhostReuse @"hostReuse"
#define kTableViewCellactive @"activeReuse"
#define kTableViewCellShowMore @"showMoreReuse"
@interface HotSearchViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *hotSearchTableView;
@property (nonatomic, strong) HotSearchRootClass *rootModel;
@end

@implementation HotSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(handleLeft:)];
    
    // Do any additional setup after loading the view from its nib.
    
    [self.hotSearchTableView registerNib:[UINib nibWithNibName:@"HostTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellhostReuse];
    
    [self.hotSearchTableView registerNib:[UINib nibWithNibName:@"ActiveTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellactive];
    
    [self.hotSearchTableView registerNib:[UINib nibWithNibName:@"ShowMoreTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellShowMore];
    
}
- (void)handleLeft:(UIBarButtonItem *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setSearchString:(NSString *)searchString {
    _searchString = searchString;
    [self.hotSearchTableView reloadData];
    [self handleData];
}

- (void)handleData {
    NSString *HTTPStr = @"http://wanapi.damai.cn/search.json?platform=4&osize=20&source=10345&psize=20&version=203004&channel_from=m360_market&pindex=0&cityId=110000";
    NSString *finalStr = [HTTPStr stringByAppendingFormat:@"&key=%@", _searchString];
    
    [CDMNetTool GET:finalStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
//        NSLog(@"请求成功: %@", result);
        self.rootModel = [HotSearchRootClass modelWithDic:result];
//        NSLog(@"%@", self.rootModel);
        [self.hotSearchTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败: %@", error);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    } else {
        return self.rootModel.data.projlist.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row < 2) {
            HostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellhostReuse];
            [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data.orglist[indexPath.row].avatar] placeholderImage:[UIImage imageNamed:@"wode_morentouxiang"]];
            cell.nameLabel.text = self.rootModel.data.orglist[indexPath.row].name;
            
        return cell;
        } else {
            ShowMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellShowMore];
            [cell clickButton:^(UIButton *clickButton) {
                self.hidesBottomBarWhenPushed = YES;
                ShowMoreViewController *showVC = [[ShowMoreViewController alloc] init];
                showVC.orglistArr = self.rootModel.data.orglist;
                [self.navigationController pushViewController:showVC animated:YES];
                self.hidesBottomBarWhenPushed = YES;
            }];
            return cell;
        }
        
    } else {
        ActiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellactive];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.rootModel.data.projlist[indexPath.row].pic] placeholderImage:[UIImage imageNamed:@"project_bg_detail"]];
        cell.titleLabel.text = self.rootModel.data.projlist[indexPath.row].title;
        cell.timeLabel.text = self.rootModel.data.projlist[indexPath.row].timestr;
        cell.addressLabel.text = self.rootModel.data.projlist[indexPath.row].address;
        
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
//        
//        if (self.rootModel.data.projlist[indexPath.row].state == 2) {
        
            DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
            UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:detailsVC];
            detailsVC.parm = [NSString stringWithFormat:@"%@", self.rootModel.data.projlist[indexPath.row].ID];
            detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

            [self presentViewController:detaNavi animated:YES completion:nil];
            
//        } else if (self.rootModel.data.projlist[indexPath.row].state == 3) {
//
//            FoundDetailsViewController *foundDetailsVC = [[FoundDetailsViewController alloc] init];
//            UINavigationController *detaNavi = [[UINavigationController alloc] initWithRootViewController:foundDetailsVC];
//            foundDetailsVC.parm = [NSString stringWithFormat:@"%@", self.rootModel.data.projlist[indexPath.row].ID];
//            detaNavi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//
//            [self presentViewController:detaNavi animated:YES completion:nil];
//            
//        }

        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    } else {
        return 100;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
